package com.fbs.job.admin.service.impl;
import com.fbs.job.admin.core.entity.FbsJobActuator;
import com.fbs.job.admin.mapper.FbsJobActuatorMapper;
import com.fbs.job.admin.service.FbsJobActuatorService;
import com.fbs.job.core.model.ResultT;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 描述：
* @author wwd
* @date 2024-01-29 13:54:43
*/

@Service
public class FbsJobActuatorServiceImpl  implements FbsJobActuatorService {

    @Resource
    private FbsJobActuatorMapper fbsJobActuatorMapper;

    @Override
    public Map<String, Object> pageList(HttpServletRequest request, int start, int length, String appName, String title) {
        // page query
        List<FbsJobActuator> list = fbsJobActuatorMapper.pageList(start-1, length, appName, title);
        int list_count = fbsJobActuatorMapper.pageListCount(start, length, appName, title);
        // package result
        Map<String, Object> maps = new HashMap<>(2);
        maps.put("recordsTotal", list_count);		// 总记录数
        maps.put("recordsFiltered", list_count);	// 过滤后的总记录数
        maps.put("code",0);
        maps.put("data", list);  					// 分页列表
        return maps;
    }

    @Override
    public FbsJobActuator getById(int id) {
        return fbsJobActuatorMapper.getById(id);
    }

    @Override
    public ResultT<String> addActuator(FbsJobActuator fbsJobActuator) {

        // valid
        if (StringUtils.isBlank(fbsJobActuator.getAppName())) {
            return ResultT.error("请填写appName");
        }
        if (fbsJobActuator.getAppName().length()<4 || fbsJobActuator.getAppName().length()>64) {
            return ResultT.error("AppName长度限制为4~64");
        }
        if (fbsJobActuator.getAppName().contains(">") || fbsJobActuator.getAppName().contains("<")) {
            return ResultT.error("AppName非法");
        }
        if (StringUtils.isBlank(fbsJobActuator.getTitle())) {
            return ResultT.error("请填写名称");
        }
        if (fbsJobActuator.getTitle().contains(">") || fbsJobActuator.getTitle().contains("<")) {
            return ResultT.error("名称非法");
        }
        if (fbsJobActuator.getAddressType()!=0) {
            if (fbsJobActuator.getAddressList()==null || fbsJobActuator.getAddressList().trim().length()==0) {
                return ResultT.error("手动录入注册方式，机器地址不可为空");
            }
            if (fbsJobActuator.getAddressList().contains(">") || fbsJobActuator.getAddressList().contains("<")) {
                return ResultT.error("机器地址格式非法");
            }

            String[] addresss = fbsJobActuator.getAddressList().split(",");
            for (String item: addresss) {
                if (item==null || item.trim().length()==0) {
                    return ResultT.error("机器地址格式非法");
                }
            }
        }
        // process
        fbsJobActuator.setUpdateTime(new Date());
        if (fbsJobActuator.getId()>0){
            int ret = fbsJobActuatorMapper.update(fbsJobActuator);
            return (ret>0)?ResultT.success(null):ResultT.error(null);
        }else{
            int ret = fbsJobActuatorMapper.save(fbsJobActuator);
            return (ret>0)?ResultT.success(null):ResultT.error(null);
        }

    }

    @Override
    public List<FbsJobActuator> findAll() {
        return fbsJobActuatorMapper.findAll();
    }

    @Override
    public int delete(int id) {
        return fbsJobActuatorMapper.delete(id);
    }
}
